package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.ResultObj;
import com.southwind.entity.ClassTable;
import com.southwind.service.ClassTableService;
import com.southwind.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-12-17
 */
@RestController
@RequestMapping("/class")
public class ClassTableController {

    @Autowired
    private ClassTableService classService;

    @GetMapping("/list")
    public PageVO list(String name,Integer page,Integer limit){
        QueryWrapper<ClassTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "class_name", name);
        Page<ClassTable> pageModel = new Page<>(page, limit);
        Page<ClassTable> resultPage = this.classService.page(pageModel, queryWrapper);
        PageVO pageVO = new PageVO();
        pageVO.setCount(resultPage.getTotal());
        pageVO.setData(resultPage.getRecords());
        return pageVO;
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody ClassTable classTable){
        boolean save = this.classService.save(classTable);
        if(save) return ResultObj.CLASS_ADD_SUCCESS;
        return ResultObj.CLASS_ADD_ERROR;
    }

    @PostMapping("/update")
    public ResultObj update(@RequestBody ClassTable classTable){
        boolean updateById = this.classService.updateById(classTable);
        if(updateById) return ResultObj.CLASS_UPDATE_SUCCESS;
        return ResultObj.CLASS_UPDATE_ERROR;
    }

    @DeleteMapping("/delete/{id}")
    public ResultObj delete(@PathVariable("id") Integer id){
        boolean removeById = this.classService.removeById(id);
        if(removeById) return ResultObj.CLASS_DELETE_SUCCESS;
        return ResultObj.CLASS_DELETE_ERROR;
    }

}

